var loading = false  //状态标记
var app = new Vue({
	el: '#app',
	data: {
		assDetails: {
			list: [],
		},
		pageIndex: 1, //当前第一页
		pageSize: 5, //每页10条
		commentList: { list: [], total: 0 },
		objectId: "",
		messageval: "",
		bottomImg: '',//广告
		types: '',
		title: '',
		userId: 0,//当前用户id
		pageHide: true,//页面显示
	},
	created: function () {
		var that = this;
		if ($.getPara("userId") != "") {
			var userId = $.getPara("userId");
			this.userId = userId;
			//缓存用户ID
			$.setStorage("userId", userId);
		} else {
			if ($.getStorage("userId") && $.getStorage("userId") != "") {
				var userId = parseInt($.getStorage("userId"));
				this.userId = userId;
			}
		}
		var objectId = $.GetRequest().id;
		var types = $.GetRequest().types;
		if ($.GetRequest().types) {  //获取types分类 
			this.types = $.GetRequest().types;
		}
		if (that.types == 1) {
			$("title").html("企业动态详情");
		}
		if (that.types == 2) {
			$("title").html("本地新闻详情");
		}
		if (that.types == 3) {
			$("title").html("协会动态详情");
		}
		if (that.types == 4) {
			$("title").html("政策动态详情");
		}
		if (that.types == 5) {
			$("title").html("人物详情");
		}
		if (that.types == 6) {
			$("title").html("访谈详情");
		}
		if (that.types == 7) {
			$("title").html("评析详情");
		}
		if (that.types == 0) {
			$("title").html("资讯详情");
		}
		if (types == 5) {
			that.title = '本地人物详情'
		}
		if (types == 6) {
			that.title = '企业访谈详情'
		}
		if (types == 7) {
			that.title = '评析详情'
		}
		that.types = 7; //评论对象类型：1故事回忆，2话题，3学院，4协会，5媒体，6心情，7动态，8评测 9活动
		if (objectId) {
			this.objectId = objectId;
			FastClick.attach(document.body);
			that.getDetails(objectId);
			that.getCommentList(objectId);
			$("#asso-news li").last().css("border-bottom", "none");
		} else {
			$.toast("没有当前页面的ID", 'text')
			this.pageHide = false;
		}
		that.getBottomImg();

	},
	mounted() {
		var that = this;
		this.$nextTick(function () {
			$(document.body).infinite().on("infinite", function () {
				if (loading) return;
				loading = true;
				that.hideing = false;
				setTimeout(function () {
					// console.log("已经加载出来了+")
					var pageIndex = that.pageIndex;
					pageIndex++;
					that.pageIndex = pageIndex;
					that.getCommentList();
					loading = false;
				}, 1500); //模拟延迟
			});
			// 此处填第一次渲染完成后执行的代码
		})
	},
	methods: {
		backTop() {
			window.history.go(-1);
		},

		//获取广告图 positionType:
		/*1.首页 3个位置
		•2.故事与回忆 详情 底部
		•3.学院 详情 底部
		•4.协会 详情 底部
		•5.媒体 详情 底部
		•6.设备 评测详情 底部
		•7.分站-主页 4个位置
		•8.分站 活动详情 底部
		•9.动态 详情 底部
		•10.人物 详情 底部
		•11.访谈 详情 底部
		•12.心情 详情 底部
		•13.活动 详情 底部
		•14.资讯 详情 底部
		•15.展会 主页 
		•16.展会 详情 底部
		*/
		getBottomImg() {
			var that = this;
			$.ajax({
				type: "GET",
				url:  "/api/banner-ad/ad/byPositionType",
				dataType: 'json',
				data: {
					positionType: 10
				},
				success: function (res) {
					if (res.data != "") {
						// console.log(res.data);
						that.bottomImg = res.data;
					}
				},
			})
		},
		//获取详情
		getDetails(id) {
			var that = this;
			var id = id;

			$.ajax({
				type: "GET",
				url:  "/api/dynamic/detail", //活动相关接口
				dataType: 'json',
				data: {
					id: id,
				},
				success: function (res) {
					if (res.code == 200) {
						res.data.keywords = JSON.parse(res.data.keywords);
						that.assDetails = res.data;
						//that.assDetails.is_collect = 0; 
						that.$set(that.assDetails, "is_collect", 0)
					} else {
						$.toast(res.msg, text);
					}

				},
			})
		},
		//评论列表
		getCommentList() {
			var that = this;
			var objectId = that.objectId;
			$.ajax({
				type: "GET",
				url:  "/api/comment/common/list",
				dataType: 'json',
				data: {
					type: that.types,  //评论对象类型：1故事回忆，2话题，3学院，4协会，5媒体，6心情，7动态，8评测 9活动
					objectId: objectId,
					userId: that.userId,
					pageIndex: that.pageIndex,
					pageSize: that.pageSize
				},
				success: function (res) {
					if (res.code == 200) {
						var list = res.data.list;
						that.commentList.total = res.data.total;
						if (that.commentList.list.length > 0) {//原来有数据，加载更多
							if (list.length > 0) {
								var original = that.commentList.list;
								var array = original.concat(list);
								that.commentList.list = array;
							} else {
								loading = true;
								$.toast("没有更多了...", "text")
							}
							//更新状态
							that.hideing = true;
						} else {//
							that.commentList.list = res.data.list;
							that.hideing = true;
						}

					} else {
						//添加数据
						$.toast(res.msg, "text");
					}
				},
				error: function (xhr, textStatus) {
				},
			})
		},
		//收藏
		chooseCollect() {
			//添加一个强制登录以获取userId
			if ($.getPara("userId") != "" && this.userId > 0) {//获取到userId字段内容并且用户id>0已登录
				var that = this;
				var objectId = $.getPara('id');
				if (that.userId != false) {
					// 收藏对象类型：1资讯，2供应，3求购，4设备，5印厂，6协会，7媒体，8学院，9评测，10动态
					$.ajax({
						type: "POST",
						url:  "/api/collect/collect",
						dataType: 'json',
						data: {
							type: 10,
							objectId: objectId,
							userId: that.userId,
						},
						success: function (res) {
							if (res.code == 200) {
								$.toast("收藏成功", "text");
								that.$set(that.assDetails, "is_collect", 1)
							}
						},
					})
				}
			} else {
				//用户未登录或者未获取到userId字段内容
				if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
					//默认为iOS系统   
					window.webkit.messageHandlers.appLogin.postMessage(null);
				} else {
					//pc
					alert("PC浏览器-appLogin");
				};
			}
		},
		//取消收藏
		chanceCollect() {
			//添加一个强制登录以获取userId
			if ($.getPara("userId") != "" && this.userId > 0) {//获取到userId字段内容并且用户id>0已登录
				var that = this;
				var objectId = $.getPara('id');
				if (that.userId != false) {
					//1资讯，2供应，3求购，4设备，5印厂
					$.ajax({
						type: "POST",
						url:  "/api/collect/cancelCollect",
						dataType: 'json',
						data: {
							type: 10,
							objectId: objectId,
							userId: that.userId,
						},
						success: function (res) {
							if (res.code == 200) {
								$.toast("取消收藏成功", "text");
								that.$set(that.assDetails, "is_collect", 0)
							}
						},
					})
				}
			} else {
				//用户未登录或者未获取到userId字段内容
				if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
					//默认为iOS系统   
					window.webkit.messageHandlers.appLogin.postMessage(null);
				} else {
					//pc
					alert("PC浏览器-appLogin");
				};
			}

		},
		//发表评论
		//评论对象类型：1故事回忆，2话题，3学院，4协会，5媒体，6心情，7动态 8评测 9活动 10印厂
		sendMessage() {
			//添加一个强制登录以获取userId
			if (this.userId && this.userId > 0) {//获取到userId字段内容并且用户id>0已登录
				var that = this;
				var val = that.messageval;
				var objectId = that.objectId;

				if (that.userId != false) {
					if (val == "") {
						$.toast("请填写评论", "text");
						return false;
					};
					$.ajax({
						type: "POST",
						url:"/api/comment/common/publish",
						dataType: 'json',
						data: {
							type: 7,
							objectId: objectId,
							userId: that.userId,
							comment: val
						},
						success: function (res) {
							if (res.code == 200) {
								$.toast("评论成功", "text");
								that.messageval = "";
								that.pageIndex = 1;//更新当前一页
								that.commentList.list = [];//清除当前的列表数据
								that.getCommentList();
							}
						},
					})
				}
			} else {//用户未登录
				if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
					//默认为iOS系统   
					window.webkit.messageHandlers.appLogin.postMessage(null);
				} else {
					//pc
					alert("PC浏览器-appLogin");
				};
			}
		},
		
		//调用原生分享
		appShare(item) {
			/*参数：
			标题：title
			内容：content
			图片：imgurl
			路径：url*/
			var para = {};
			var that = this;
			para.title = item.title;
			para.content = "来自印刷搜搜的分享";
			para.imgurl = item.head_img;
			$("title").html(item.title);
			/*
				分享出去的页面仅做展示效果，无法处理用户登录操作
				因此，userId需要传0
				isShare为是否分享标识，1:是分享页面，
				H5端要在有分享功能的页面用url的isShare==1判断是否隐藏需要登录才能显示或者操作的区域
			*/
			para.url = '/PageClassify/html/Substation/Character/subCharacterDetails.html?isShare=1&id=' + item.id + "&userId=0&types=" + that.types;
			//window.location.href = para.url;
			if (/(Android)/i.test(navigator.userAgent)) { //判断Android
				//是android系统  目前协议 安卓只能传字符串过去
				// alert("Android系统");
				jsAndroid.appShare(JSON.stringify(para))
				// jsAndroid.appPush("123456")
			} else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
				//默认为iOS系统   
				window.webkit.messageHandlers.appShare.postMessage(para);
			} else {
				//pc
				window.location.href = para.url;
				alert("PC浏览器");
			};
		}
	}
})

